<template>
  <div class="spare-parts-page">
    <div class="page-header">
      <h2>
        <el-icon><Box /></el-icon>
        备件库存管理
      </h2>
      <div class="header-actions">
        <el-button type="primary" @click="handleAddPart">
          <el-icon><Plus /></el-icon>
          新增备件
        </el-button>
        <el-button @click="handleImport">
          <el-icon><Upload /></el-icon>
          导入
        </el-button>
      </div>
    </div>

    <!-- 库存预警 -->
    <el-row :gutter="20" class="alert-row">
      <el-col :span="24">
        <el-alert 
          title="库存预警" 
          type="warning" 
          :closable="false"
          v-if="lowStockCount > 0"
        >
          <div>有 <strong>{{ lowStockCount }}</strong> 个备件库存低于安全库存，请及时采购</div>
        </el-alert>
      </el-col>
    </el-row>

    <!-- 备件列表 -->
    <el-card>
      <template #header>
        <div class="card-header">
          <span>备件列表</span>
          <el-input 
            v-model="searchText" 
            placeholder="搜索备件..." 
            clearable
            style="width: 300px"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </div>
      </template>

      <el-table :data="filteredPartsList" stripe border>
        <el-table-column prop="code" label="备件编号" width="130" />
        <el-table-column prop="name" label="备件名称" min-width="150" />
        <el-table-column prop="specification" label="规格型号" width="150" />
        <el-table-column prop="applicableEquipment" label="适用设备" min-width="150" />
        <el-table-column prop="stock" label="当前库存" width="100" align="right">
          <template #default="{ row }">
            <span :class="{ 'text-warning': row.stock <= row.safetyStock }">
              {{ row.stock }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="safetyStock" label="安全库存" width="100" align="right" />
        <el-table-column prop="unit" label="单位" width="80" />
        <el-table-column prop="price" label="单价" width="100" align="right">
          <template #default="{ row }">
            ¥{{ row.price }}
          </template>
        </el-table-column>
        <el-table-column label="库存状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStockStatusColor(row)">
              {{ getStockStatus(row) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" text @click="handleInOut(row)">
              出入库
            </el-button>
            <el-button type="success" size="small" text @click="handlePurchase(row)">
              采购
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 出入库对话框 -->
    <el-dialog v-model="showInOutDialog" title="备件出入库" width="600px">
      <el-form :model="inOutForm" label-width="100px">
        <el-form-item label="操作类型">
          <el-radio-group v-model="inOutForm.type">
            <el-radio label="in">入库</el-radio>
            <el-radio label="out">出库</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="数量">
          <el-input-number v-model="inOutForm.quantity" :min="1" style="width: 100%" />
        </el-form-item>
        <el-form-item label="领用设备" v-if="inOutForm.type === 'out'">
          <el-select v-model="inOutForm.equipmentId" placeholder="请选择" style="width: 100%">
            <el-option label="数控车床 EQ-CNC-001" value="1" />
            <el-option label="加工中心 EQ-CNC-002" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="经办人">
          <el-select v-model="inOutForm.handler" placeholder="请选择" style="width: 100%">
            <el-option label="张三" value="user1" />
            <el-option label="李四" value="user2" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="inOutForm.remark" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showInOutDialog = false">取消</el-button>
        <el-button type="primary" @click="handleSubmitInOut">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { Tools, Plus, Box, Upload, Search } from '@element-plus/icons-vue'

const showInOutDialog = ref(false)
const searchText = ref('')

const statistics = reactive({
  pending: 5,
  repairing: 3,
  thisMonth: 28,
  avgTime: 4.5
})

const partsList = ref([
  {
    id: 1,
    code: 'SP-001',
    name: '主轴轴承',
    specification: 'SKF 6208',
    applicableEquipment: '数控车床',
    stock: 5,
    safetyStock: 10,
    unit: '个',
    price: 350
  },
  {
    id: 2,
    code: 'SP-002',
    name: '液压油',
    specification: '46#抗磨',
    applicableEquipment: '冲压机',
    stock: 15,
    safetyStock: 20,
    unit: '升',
    price: 35
  },
  {
    id: 3,
    code: 'SP-003',
    name: '伺服电机',
    specification: '1.5KW',
    applicableEquipment: '加工中心',
    stock: 25,
    safetyStock: 5,
    unit: '台',
    price: 2800
  }
])

const repairList = ref([])

const inOutForm = reactive({
  type: 'out',
  quantity: 1,
  equipmentId: '',
  handler: '',
  remark: ''
})

const filteredPartsList = computed(() => {
  if (!searchText.value) return partsList.value
  return partsList.value.filter(part => 
    part.name.includes(searchText.value) || 
    part.code.includes(searchText.value)
  )
})

const lowStockCount = computed(() => {
  return partsList.value.filter(part => part.stock <= part.safetyStock).length
})

const getStockStatus = (row) => {
  if (row.stock === 0) return '缺货'
  if (row.stock <= row.safetyStock) return '预警'
  return '正常'
}

const getStockStatusColor = (row) => {
  if (row.stock === 0) return 'danger'
  if (row.stock <= row.safetyStock) return 'warning'
  return 'success'
}

const getLevelColor = () => ''
const getStatusName = () => ''
const getStatusColor = () => ''

const handleAddPart = () => {
  ElMessage.info('新增备件功能开发中')
}

const handleImport = () => {
  ElMessage.info('导入功能开发中')
}

const handleInOut = (row) => {
  showInOutDialog.value = true
}

const handlePurchase = (row) => {
  ElMessage.info(`发起采购申请：${row.name}`)
}

const handleSubmitInOut = () => {
  showInOutDialog.value = false
  ElMessage.success('出入库成功')
}

const handleCreateRepair = () => {}
</script>

<style scoped>
.spare-parts-page {
  padding: 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.alert-row {
  margin-bottom: 20px;
}

.stats-row {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text-warning {
  color: #e6a23c;
  font-weight: 600;
}
</style>

